package com.globalwave.filemanage.text.analyzer;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;


import com.globalwave.common.cache.CodeHelper;
import com.globalwave.filemanage.FMCodeHelper;
import com.globalwave.util.LocaleUtil;

/**
 * Class for analysing texts like extracting tags from a given text. Created on
 * 24.03.2004
 * 
 * @author Michael Scholz
 */
public class AnalyzerManager {

	//private Map<Locale, AnalyzerAdapter> analyzers;

	public AnalyzerManager() {
	}
	
/*
	private void init() throws Exception {

		this.analyzers = new HashMap<Locale, AnalyzerAdapter>();
		
        Map<Object, Object> analyzers = FMCodeHelper.getAnalyzerMap() ;
      
        for (Map.Entry<Object, Object> analyzer : analyzers.entrySet()) {
      	
			String language = (String)analyzer.getKey() ;
			String analyzerClass = (String)analyzer.getValue();


				//if (StringUtils.isNotEmpty(analyzerClass) && !"-".equals(analyzerClass))
				//	lang.setAnalyzerClass(analyzer);
			this.analyzers.put(
					LocaleUtil.toLocale(language), 
					new AnalyzerAdapter(language, 4));

		}

	}
	*/

	public AnalyzerAdapter getAnalyzer(String locale) {
/*
		if ((locale == null) || (!analyzers.containsKey(locale)))
			return analyzers.get(Locale.ENGLISH);
		
		return analyzers.get(locale);*/
		return new AnalyzerAdapter(locale) ;
	}

	/**
	 * This method extracts a specified number of tags and appends them to a
	 * String
	 * 
	 * @param count Number of tags.
	 * @param text Given text of a document.
	 * @param language Identified language of the text.
	 * @return String of tags like "Information, Retrieval, DMS, CMS"
	 * @throws Exception
	 */
	public String getTermsAsString_4_delete(int count, String text, String locale) throws Exception {
		StringBuffer result = new StringBuffer();
		AnalyzerAdapter analyzer = getAnalyzer(locale);
		analyzer.analyze(text);

		Collection<Entry> terms = analyzer.getTopWords(count);
		Iterator<Entry> iter = terms.iterator();
		int temp = 0;

		while (iter.hasNext() && (temp < count)) {
			Entry entry = iter.next();

			if (temp > 0) {
				result.append(", ");
			}

			result.append(entry.getOriginWord());
			temp++;
		}

		return result.toString();
	}

}